package com.neu.servlet.ww;

import com.alibaba.fastjson.JSONObject;
import com.neu.service.ww.GoodsService;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet(name = "JoinCarServlet",urlPatterns = "/JoinCarServlet.do")
public class JoinCarServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String g_id=request.getParameter("g_id");
        System.out.println(g_id);
        String num=request.getParameter("num");
        System.out.println(num);
        HttpSession session=request.getSession();
        List<Map<String, Object>> carList= (List<Map<String, Object>>) session.getAttribute("carList");
        GoodsService goodsService=new GoodsService();
        //更新商品库存
        int row=goodsService.doUpdateNumById(g_id,num);
        System.out.println(row);
        //查询商品信息
        Map<String,Object> goodMap=goodsService.doFindById(g_id);
        Map<String,Object> numMap=new HashMap<>(16);
        PrintWriter pw=response.getWriter();
        if (goodMap!=null&&!goodMap.isEmpty()&&row>0&&Integer.parseInt(goodMap.get("G_NUM").toString())>0){
            if (carList.isEmpty()){
                goodMap.put("num", 1);
                carList.add(goodMap);
            }else {
                for (int i = 0; i < carList.size(); i++) {
                    Map<String, Object> aCarList = carList.get(i);
                    System.out.println(aCarList.get("G_ID").toString().equals(g_id));
                    if (aCarList.get("G_ID").toString().equals(g_id)) {
                        if ((Integer)aCarList.get("num")>=10){
                            break;
                        }else {
                            aCarList.put("num", (Integer)aCarList.get("num")+1);
                            break;
                        }
                    }
                    if (i == carList.size()-1) {
                        goodMap.put("num", 1);
                        carList.add(goodMap);
                        i++;
                    }
                }
            }

            System.out.println(carList);
            session.setAttribute("carList",carList);
            numMap.put("g_num",goodMap.get("G_NUM"));
            String json=JSONObject.toJSONString(numMap);
            pw.write(json);
            pw.close();
        }else {
            numMap.put("g_num","库存不足");
            String json=JSONObject.toJSONString(numMap);
            pw.write(json);
            pw.close();
        }

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
